/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyectoprogracuestionario;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.border.TitledBorder;

/**
 *
 * @author Diego
 */
public class PreCombobox extends JPanel implements ActionListener{

    private JPanel pnlPreguntaCmb,pnlPregunta,pnlSolu,pnlOK,pnl13,pnl12,pnl14,pnl11;
    private JTextField txtTituloP,txtayuda,txtSoluc,txtSE;
    private JLabel lblTituP,lblTextAyu,lblSolu,lblEliminar,lblNtitu,lblNtext;
    private JButton btnAgregar,btnEliminar,btnAceptar,btnEviarP,btnCancelaP;
    private JComboBox cmbRespuestas;
    private ImageIcon img,img2;
    private String TipoPregunta,tituloP,textA;
    private ArrayList<String>solucion= new ArrayList();
    private VentFormulario form;
    
    public PreCombobox() {
        
        this.setSize(720, 400);
        //this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(pnlPreguntaCmb= new JPanel());
        pnlPreguntaCmb.setBorder(new TitledBorder("Pregunta"));
        pnlPreguntaCmb.setLayout(new BoxLayout(pnlPreguntaCmb,BoxLayout.Y_AXIS ));
        
        JPanel pnl0, pnl1,pnl2;
        pnlPreguntaCmb.add(pnlPregunta= new JPanel());
        pnlPregunta.add(pnl0= new JPanel());        
        pnl0.setLayout(new BoxLayout(pnl0, BoxLayout.Y_AXIS));
        pnl0.add(lblNtitu= new JLabel());
        lblNtitu.setVisible(false);
        pnl0.add(lblTituP= new JLabel("Titulo de la Pregunta"));
        pnl0.add(txtTituloP= new JTextField(20));
        pnl0.add(lblNtext= new JLabel());
        lblNtext.setVisible(false);
        pnl0.add(lblTextAyu= new JLabel(" Texto de Ayuda        "));        
        pnl0.add(txtayuda= new JTextField(20));
//        pnlPregunta.add(pnl1= new JPanel());
//        pnl1.add(lblTituP= new JLabel("Titulo de la Pregunta"));
//        pnl1.add(txtTituloP= new JTextField(20));
//        pnlPregunta.add(pnl2= new JPanel());
//        pnl2.add(lblTextAyu= new JLabel(" Texto de Ayuda        "));
//        pnl2.add(txtayuda= new JTextField(20));
       
        pnlPreguntaCmb.add(pnlSolu= new JPanel());
        pnlSolu.add(pnl11= new JPanel());
        pnl11.setLayout(new BoxLayout(pnl11, BoxLayout.Y_AXIS));
        pnl11.add(pnl12= new JPanel());
        pnl12.add(lblSolu= new JLabel("Posible Solucion"));
        pnl12.add(txtSoluc= new JTextField(20));
        pnl12.add(btnAgregar= new JButton("Agregar"));
        pnl12.add(btnEliminar= new JButton("Elimninar"));
        
        pnl11.add(pnl13= new JPanel());
        pnl13.add(lblEliminar= new JLabel("Solucion Eliminar"));
        pnl13.add(txtSE= new JTextField(20));
        pnl13.add(btnAceptar= new JButton("Aceptar"));
        
        pnl11.add(pnl14= new JPanel());
        pnl14.add(cmbRespuestas= new JComboBox());
        
//        pnlPreguntaCmb.add(pnlOK= new JPanel());
//         img=new ImageIcon("X11.png");
//        pnlOK.add(btnEviarP= new JButton("Enviar Pregunta"));
//        pnlOK.add(btnCancelaP= new JButton("Cancelar"));
        
        btnAgregar.addActionListener(this);
        btnEliminar.setVisible(false);
        pnl13.setVisible(false);
        btnEliminar.addActionListener(this);
        btnAceptar.addActionListener(this);
       
        tituloP="";
        textA="";
    }
    
    
    
    
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btnAgregar){
           //solucion.removeAll(solucion); 
           String solu=txtSoluc.getText();
           solucion.add(solu);
           llenarcombo(); 
           btnEliminar.setVisible(true);
           txtSoluc.setText(null);
           txtSoluc.requestFocus();
        }
        
        if(e.getSource()==btnEliminar){
            pnl13.setVisible(true);
           
        }
        
         if(e.getSource()==btnAceptar){
                String eli=txtSE.getText();
                for(int i=0;i<solucion.size();i++){
                    
                    if(solucion.get(i).equalsIgnoreCase(eli)) {
                        solucion.remove(i);                        
                        llenarcombo(); 
                        pnl13.setVisible(false);
                    }
                    
                }
                
            }
         
         if(e.getSource()==btnCancelaP){
             solucion.removeAll(solucion);
             cmbRespuestas.removeAllItems();
             //dispose();             
         }
        
         if(e.getSource()==btnEviarP){
             TipoPregunta="ComboBox";
//             this.tituloP=txtTituloP.getText();
//             
//              System.out.println(tituloP);
//             textA=txtayuda.getText();
//             System.out.println(textA);
             VentFormulario v1= new VentFormulario(txtTituloP.getText(),txtayuda.getText(),TipoPregunta);             
             v1.setVisible(true);
           //  dispose();
             //v1.enviarPre(txtTituloP.getText(), txtayuda.getText(), pnl14);
         }
        
    }
    
    public void llenarcombo(){
        cmbRespuestas.removeAllItems();
        for(int i=0;i<solucion.size();i++){
            cmbRespuestas.addItem(solucion.get(i));
        }
    }
    
    public void vista(){
//        JPanel pnlTitu,pnl1,pnl2,pnl3;
//        JLabel lbltitu,lbltexta;
//        pnlTitu= new JPanel();
//        pnlTitu.add(pnl1= new JPanel());
//        pnlTitu.setLayout(new BoxLayout(pnlTitu,BoxLayout.Y_AXIS));
//        pnl1.setLayout(new GridLayout(2,1,5,5));
//        pnl1.add(lbltitu= new JLabel(txtTituloP.getText()));
//        pnl1.add(lbltexta= new JLabel(txtayuda.getText()));
//        pnlTitu.add(pnl2= new JPanel());
//        pnl2.add(cmbRespuestas);
          lblNtitu.setText(txtTituloP.getText());
          lblNtitu.setVisible(true);
          lblTituP.setVisible(false);
          txtTituloP.setVisible(false);
          pnl13.setVisible(false);
          pnl12.setVisible(false);
          lblNtext.setText(txtayuda.getText());
          lblNtext.setVisible(true);
          lblTextAyu.setVisible(false);
          txtayuda.setVisible(false);
          
          
          
        
    }
    
       public void llenarcomboextra(JComboBox aux){
        aux.removeAllItems();
        for(int i=0;i<solucion.size();i++){
            aux.addItem(solucion.get(i));
        }
    }

    public ArrayList<String> getSolucion() {
        return solucion;
    }
    public String getTituloP(){
        return tituloP;
    }
    
}
